'''
应医生要求，将医生的人工预测结果和xgboost预测结果集成
生成文件转发给医生由医生分析其中差别
这里只集成了二分类结果

'''


from sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc, precision_recall_curve, average_precision_score
import numpy as np
import pandas as pd
import csv

# 经过绘制roc曲线观察，取0.25做二分类阈值
def get_xgboost_result():
    wh_xgboost_labels = np.load('D:/lung_cancer/data/two_result/two_xgboost_wh_labels.npy')
    wh_xgboost_preds = np.load('D:/lung_cancer/data/two_result/two_xgboost_wh_preds.npy')
    for i in range(len(wh_xgboost_preds)):
        if wh_xgboost_preds[i] > 0.25:
            wh_xgboost_preds[i] = 1
        else:
            wh_xgboost_preds[i] = 0
    return wh_xgboost_preds

# 将医生诊断的腺癌和鳞癌结果提取出来，寻找到真实标签，并保存为doctor_two_result.csv
def divide_two_result():
    labels_data = open('D:/lung_cancer/data/divide_csv/two/test.csv')
    labels_read_lines = csv.reader(labels_data)
    data1 = []
    for line in labels_read_lines:
        data1.append(line)

    doctor_csv_file = open('D:/lung_cancer/data/doctor_result.csv')

    csv_reader_lines = csv.reader(doctor_csv_file)
    data2 = []
    for one_line in csv_reader_lines:
        data2.append(one_line)
    test_list = []

    # 统计腺癌和鳞癌的测试集数量
    for i in range(1, len(data2)):
        for j in range(1, len(data1)):
            if str(data2[i][0]) == str(data1[j][1]):
                test_list.append([data2[i][0], data1[j][6], data2[i][1]])


    df = pd.DataFrame(test_list, columns=['patientID', 'label', 'doctor_result'])
    df.to_csv('D:/lung_cancer/data/two_result.csv', index=False)

# 读取数据
def read_data(data_path):
    data = []
    f = csv.reader(open(data_path, 'r'))
    for i in f:
        data.append(i)
    return data

# 把xgboost结果补充进csv文件
def add_xbg_result():
    data_path = 'D:/lung_cancer/data/two_result.csv'
    data = read_data(data_path)

    xgb_pred = get_xgboost_result()
    data[0].append('xbg_result')
    for i in range(1, len(data)):
        data[i].append(xgb_pred[i-1]+1)

    df = pd.DataFrame(data[1:], columns=data[0])
    df.to_csv('D:/lung_cancer/data/two_result.csv', index=False)


# xgboost取0.25做阈值
if __name__ == '__main__':
    divide_two_result()
    add_xbg_result()